import axios from "axios";
import {message } from 'antd';
import { useSelector } from 'react-redux'

import {store} from '../../store/index'

 let state = store.getState()
 console.log(state)

let service = axios.create({
    timeout:10*1000,
    baseURL:'/api'
})
service.interceptors.request.use(
    (config)=>{
        config.headers["username"] = state.username
        return config
    },
    (error)=>{
        return Promise.reject(error)
    },
)
service.interceptors.response.use(
    (res)=>{
        return res
    },
    (error)=>{
        if(error.response.status == 401){
            message.error({content:'登录过期'})
        }else if(error.response.status == 404){
            message.error({content:'访问路径有误'})
        }else if(error.response.status == 500){
            message.error({content:'服务器内部错误'})
        }else if(error.response.status == 503){
            message.error({content:'服务器无法提供服务'})
        }

        return Promise.reject(error)
    },
)
export default service